// CSVDataDbHelper.java
package com.example.qrcodecamera;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;

public class CSVDataDbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "CSVData.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "csv_data";

    // 字段定义
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_GOODS_NAME = "goods_name";
    public static final String COLUMN_STAFF_ID = "staff_id";
    public static final String COLUMN_OPERATOR = "operator";
    public static final String COLUMN_BATCH = "batch";
    public static final String COLUMN_MATERIAL_ID = "material_id";
    public static final String COLUMN_GROSS_WEIGHT = "gross_weight";
    public static final String COLUMN_TARE_WEIGHT = "tare_weight";
    public static final String COLUMN_NET_WEIGHT = "net_weight";
    public static final String COLUMN_TIMESTAMP = "timestamp";

    public CSVDataDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " ("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_GOODS_NAME + " TEXT,"
                + COLUMN_STAFF_ID + " TEXT,"
                + COLUMN_OPERATOR + " TEXT,"
                + COLUMN_BATCH + " TEXT,"
                + COLUMN_MATERIAL_ID + " TEXT,"
                + COLUMN_GROSS_WEIGHT + " REAL,"
                + COLUMN_TARE_WEIGHT + " REAL,"
                + COLUMN_NET_WEIGHT + " REAL,"
                + COLUMN_TIMESTAMP + " TEXT)";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    // 插入CSV数据
    public void insertCSVData(List<CSVRecord> records) {
        SQLiteDatabase db = this.getWritableDatabase();
        for (CSVRecord record : records) {
            ContentValues values = new ContentValues();
            values.put(COLUMN_GOODS_NAME, record.getGoodsName());
            values.put(COLUMN_STAFF_ID, record.getStaffId());
            values.put(COLUMN_OPERATOR, record.getOperator());
            values.put(COLUMN_BATCH, record.getBatch());
            values.put(COLUMN_MATERIAL_ID, record.getMaterialId());
            values.put(COLUMN_GROSS_WEIGHT, record.getGrossWeight());
            values.put(COLUMN_TARE_WEIGHT, record.getTareWeight());
            values.put(COLUMN_NET_WEIGHT, record.getNetWeight());
            values.put(COLUMN_TIMESTAMP, record.getTimestamp());
            db.insert(TABLE_NAME, null, values);
        }
    }

    // 分页查询
    public List<CSVRecord> getPagedData(int page, int pageSize) {
        List<CSVRecord> data = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(
                "SELECT * FROM " + TABLE_NAME
                        + " LIMIT " + pageSize
                        + " OFFSET " + (page-1)*pageSize, null);

        if (cursor.moveToFirst()) {
            do {
                CSVRecord record = new CSVRecord(
                        cursor.getInt(0),      // id
                        cursor.getString(1),   // goods_name
                        cursor.getString(2),   // staff_id
                        cursor.getString(3),   // operator
                        cursor.getString(4),   // batch
                        cursor.getString(5),   // material_id
                        cursor.getDouble(6),   // gross_weight
                        cursor.getDouble(7),   // tare_weight
                        cursor.getDouble(8),   // net_weight
                        cursor.getString(9)    // timestamp
                );
                data.add(record);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return data;
    }

    // 获取总记录数
    public int getTotalCount() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + TABLE_NAME, null);
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        return count;
    }

    public boolean deleteRecord(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        try {
            int rowsAffected = db.delete(TABLE_NAME, "id = ?", new String[]{String.valueOf(id)});
            return rowsAffected > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            db.close();
        }
    }

    //根据物料编号查询记录
    public List<CSVRecord> getRecordsByMaterialId(String materialId) {
        List<CSVRecord> records = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(
                "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_MATERIAL_ID + " = ?",
                new String[]{materialId}
        );

        if (cursor.moveToFirst()) {
            do {
                CSVRecord record = new CSVRecord(
                        cursor.getInt(0),      // id
                        cursor.getString(1),   // goods_name
                        cursor.getString(2),   // staff_id
                        cursor.getString(3),   // operator
                        cursor.getString(4),   // batch
                        cursor.getString(5),   // material_id
                        cursor.getDouble(6),   // gross_weight
                        cursor.getDouble(7),   // tare_weight
                        cursor.getDouble(8),   // net_weight
                        cursor.getString(9)    // timestamp
                );
                records.add(record);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return records;
    }
}